package com.lute.model;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.OneToOne;

import com.lute.utils.DAOFactory;


@Entity
@DiscriminatorValue("ClientAdmin")
public class ClientAdmin extends User  {
	
	private static final long serialVersionUID = 1L;
	
	private ClientAdminProfile clientAdminProfil;
	
	public ClientAdmin() {}
	public ClientAdmin(String firstName, String lastName, String login, String email, Role role) {
		super(firstName,lastName,login,email,role);
	}
	
	@OneToOne(cascade=CascadeType.ALL, mappedBy="clientAdmin")
	public ClientAdminProfile getClientAdminProfil() {
		return clientAdminProfil;
	}
	public void setClientAdminProfil(ClientAdminProfile clientAdminProfil) {
		this.clientAdminProfil = clientAdminProfil;
	}
	
	// 											("id_user", sessionClientAdminId , employeeId)
	public static boolean checkPrivilege(String propertyName, int propertyValue, int userId) {
		boolean result = false;
		ClientAdmin clientAdmin = (ClientAdmin)DAOFactory.getInstance().getLuteInterface().getClientAdminFromDataBaseByIdWithClientsClientAccPeriodsEmployeeAccPeriodsEmployees(propertyName, propertyValue);
		List<Client> clientList = clientAdmin.getClientAdminProfil().getClients();
		
		for (Client client : clientList) {
			List<ClientAccountingPeriod> clientAccPeriodList = client.getClientAccountingPeriods();
			
			for (ClientAccountingPeriod clientAccountingPeriod : clientAccPeriodList) {
				List<EmployeeAccountingPeriod> employeeAccPeriodList = clientAccountingPeriod.getEmployeeAccountingPeriod();
				
				for (EmployeeAccountingPeriod employeeAccountingPeriod : employeeAccPeriodList) {
					if (employeeAccountingPeriod.getEmployee() != null){
						if (employeeAccountingPeriod.getEmployee().getId_user() == userId){
							result = true;
						}
					}

					if (employeeAccountingPeriod.getApprover() != null){
						if (employeeAccountingPeriod.getApprover().getId_user() == userId){
							result = true;
						}
					}
				}
			}
		}
		
		return result;
	}
	
	public static ClientAdmin getClientAdminFromDB(String propertyName, int propertyValue) {
		return DAOFactory.getInstance().getLuteInterface().getClientAdminFromDataBaseById(propertyName, propertyValue);
	}

	public static ClientAdmin getClientAdminFromDBWithClients(String propertyName, int propertyValue) {
		return DAOFactory.getInstance().getLuteInterface().getClientAdminFromDataBaseByIdWithClients(propertyName, propertyValue);
	}
	
	public static List<ClientAdmin> getClientAdminByAccLockedFromDB(String propertyName, String propertyValue) {
		return DAOFactory.getInstance().getLuteInterface().getClientAdminFromDataBaseByAccLocked(propertyName, propertyValue);
	}
}
